/*
 * @Author: szx
 * @Date: 2022-01-11 14:08:12
 * @LastEditTime: 2022-01-11 20:32:02
 * @Description:
 * @FilePath: \leetcode\1-99\63\63.js
 */
/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
var uniquePathsWithObstacles = function (obstacleGrid) {
    if (obstacleGrid[0][0] == 1) return 0;
    const m = obstacleGrid.length;
    const n = obstacleGrid[0].length;
    const dp = new Array(m).fill(1).map(() => new Array(n).fill(1));
    for (let i = 1; i < m; i++) {
        if (dp[i - 1][0] == 0 || obstacleGrid[i][0] == 1) {
            dp[i][0] = 0;
        }
    }
    for (let j = 1; j < n; j++) {
        if (dp[0][j - 1] == 0 || obstacleGrid[0][j] == 1) {
            dp[0][j] = 0;
        }
    }
    for (let i = 1; i < m; i++) {
        for (let j = 1; j < n; j++) {
            if (obstacleGrid[i][j] == 1) dp[i][j] = 0;
            else dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
        }
    }
    return dp[m - 1][n - 1];
};
